package com.snail.billing.page;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import com.android.util.ResUtil;
import com.snail.billing.Billing;
import com.snail.billing.BillingCallback;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.data.Account;
import com.snail.billing.data.AccountManager;
import com.snail.billing.net.BillingEncode;
import com.snail.billing.os.DialogPage;

public class DialogUpdatePage extends DialogPage implements View.OnClickListener {

	public static final String REG = "register";
	private View button;
	private Account account;
	private Handler handler;

	private String regBool = "0";

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_dialog_update);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getPageArgs() != null && getPageArgs().containsKey(REG)) {
			regBool = getPageArgs().getString(REG);
		}
		account = AccountManager.getCurrentAccount();
		button = findViewById(ResUtil.getViewId(Resource.id.snailbilling_dialog_update_button));
		button.setOnClickListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		handler = new Handler(Looper.getMainLooper());
		handler.postDelayed(waitingRunnable, 3000);
	}

	@Override
	public void onPause() {
		super.onPause();
		handler.removeCallbacks(waitingRunnable);
	}

	@Override
	public LayoutParams getLayoutParams() {
		int orientation = getContext().getResources().getConfiguration().orientation;
		int screenWidth = getContext().getResources().getDisplayMetrics().widthPixels;
		int screenHeight = getContext().getResources().getDisplayMetrics().heightPixels;
		if (orientation == Configuration.ORIENTATION_PORTRAIT) {
			int width = (int) (screenWidth * 0.9);
			int height = (int) (screenHeight * 0.6);
			return new ViewGroup.LayoutParams(width, height);
		} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
			int width = (int) (screenWidth * 0.6);
			int height = (int) (screenWidth * 0.6 * 9 / 16);
			return new ViewGroup.LayoutParams(width, height);
		}
		return new ViewGroup.LayoutParams(screenWidth, screenHeight);
	}

	@Override
	public void onClick(View v) {
		getPageManager().finish();
		DataCache.getInstance().importParams.pageClass = BindAccountPage.class;
		getContext().startActivity(new Intent(getContext(), Billing.class));
	}

	protected Runnable waitingRunnable = new Runnable() {

		@Override
		public void run() {
			getPageManager().finish();
			DataCache.getInstance().dialogAccount = account;
			if (DataCache.getInstance().callbackIsMd5) {
				String action = BillingCallback.ACTION_LAUNCH_PWD_MD5;
				DataCache.getInstance().importParams.billingCallback.onCallback(1, action, new String[] { account.getAccount(), BillingEncode.MD5(account.getPwd()), account.getType(), regBool });
			} else {
				String action = BillingCallback.ACTION_LAUNCH;
				DataCache.getInstance().importParams.billingCallback.onCallback(1, action, new String[] { account.getAccount(), account.getPwd(), account.getType(), regBool });
			}
		}
	};

}
